package com.liyiqing.secondhandmall.memberservice.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liyiqing.commonservice.utils.NowTime;
import com.liyiqing.commonservice.utils.PageUtils;
import com.liyiqing.commonservice.utils.R;
import com.liyiqing.commonservice.validator.methodgroup.AddGroup;
import com.liyiqing.commonservice.validator.methodgroup.DeleteGroup;
import com.liyiqing.commonservice.validator.methodgroup.UpdateGroup;
import com.liyiqing.commonservice.vo.MemberCollectSpuVo;
import com.liyiqing.commonservice.vo.ProductShowVo;
import com.liyiqing.secondhandmall.memberservice.feign.ProductFeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.liyiqing.secondhandmall.memberservice.entity.MemberUserCollectSpuEntity;
import com.liyiqing.secondhandmall.memberservice.service.MemberUserCollectSpuService;

import javax.annotation.Resource;


/**
 * 某账号收藏的商品
 *
 * @author LiYiQing
 * @email 1570865668@qq.com
 * @date 2022-02-18 22:47:22
 */
@RestController
@RequestMapping("memberService/memberUserCollectSpu")
public class MemberUserCollectSpuController {
    @Autowired
    private MemberUserCollectSpuService memberUserCollectSpuService;

    //注入商品的远程服务
    @Resource
    private ProductFeignService productFeignService;



    /**
     * 将商品加入购物车
     */
    @RequestMapping("/insertCollectSpu")
    public R insertCollectSpu(@Validated({AddGroup.class}) @RequestBody MemberUserCollectSpuEntity memberUserCollectSpu){

        //创建 返给前端的对象
        MemberCollectSpuVo memberCollectSpuVo = new MemberCollectSpuVo();

        //获得前端传来的 商品id 和 卖家账号id
        Long spuId = memberUserCollectSpu.getSpuId();
        Long memberId = memberUserCollectSpu.getMemberId();

        //如果买家账号id和商品id均不为空
        if (memberId != null && spuId != null){
            //调用 远程的商品服务，通过商品id 获取该商品的简要信息
            R r = productFeignService.selectShowProductById(spuId);
            Object o = r.get("data");   //获取data中的数据
            JSONObject json = (JSONObject) JSON.toJSON(o);//将其转为json对象
            //将json对象还原成实体类对象
            ProductShowVo productShowVo = JSONObject.parseObject(String.valueOf(json), ProductShowVo.class);
            //获取 该商品图片的url
            String imageUrl = productShowVo.getImage();
            //获取 当前系统时间
            String time = NowTime.getNowTime();

            //先查看 此账号，是否已经 收藏过此商品
            MemberUserCollectSpuEntity userCollect = memberUserCollectSpuService.selectRecordByMidAndSid(memberId,spuId);
            //如果已经存在收藏记录，只需更新 图片url 和 收藏时间
            if (userCollect != null){
                userCollect.setSpuImg(imageUrl);
                userCollect.setCreateTime(time);
                //去数据库更新
                memberUserCollectSpuService.updateById(userCollect);

                //为前端展示对象 拷贝一份重复的属性
//                BeanUtils.copyProperties(userCollect,memberCollectSpuVo);
                memberCollectSpuVo = memberUserCollectSpuService.createMemberCollectSpuVo(userCollect);
            }
            //如果不存在收藏记录，就向数据库中插入数据
            else {
                //处理将要插入数据库中的数据
                memberUserCollectSpu.setSpuImg(imageUrl);//设置图片的url
                memberUserCollectSpu.setCreateTime(time);//设置收藏时间
                memberUserCollectSpu.setStatus(1);//设置收藏状态
                //将处理好的收藏记录写入数据库
                memberUserCollectSpuService.save(memberUserCollectSpu);

                //为前端展示对象 拷贝一份重复的属性
//                BeanUtils.copyProperties(memberUserCollectSpu,memberCollectSpuVo);
                memberCollectSpuVo = memberUserCollectSpuService.createMemberCollectSpuVo(memberUserCollectSpu);

            }

            //2.处理展示给前端的memberCollectSpuVo对象
//            memberCollectSpuVo.setSpuImg(imageUrl);//添加图片url
//            memberCollectSpuVo.setSpuName(productShowVo.getName());//添加商品名称
//            memberCollectSpuVo.setDescription(productShowVo.getDescription());//添加商品描述
//            memberCollectSpuVo.setGroupName(productShowVo.getGroupName());//添加商品二级分类名称
//            memberCollectSpuVo.setPrice(productShowVo.getPrice());//添加商品单价

        }

        return R.ok().put("data",memberCollectSpuVo);
    }




    /**
     * 根据账号id，获取该账号收藏的所有商品信息
     */
    @RequestMapping("/selectRecordByMemberId/{id}")
    public R selectRecordByMemberId(@PathVariable("id") Long id){
        return memberUserCollectSpuService.selectRecordByMemberId(id);
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated({UpdateGroup.class})@RequestBody MemberUserCollectSpuEntity memberUserCollectSpu){
		memberUserCollectSpuService.updateById(memberUserCollectSpu);

        return R.ok();
    }


    /**
     * 根据主键id 删除购物车记录
     */
    @RequestMapping("/deleteById/{id}")
    public R deleteById(@Validated({DeleteGroup.class})@PathVariable Long id){
        memberUserCollectSpuService.removeById(id);
        return R.ok();
    }


    /**
     * 根据主键id 批量删除购物车记录
     */
    @RequestMapping("/deleteByIds")
    public R deleteByIds(@RequestBody Long[] ids){
		memberUserCollectSpuService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberUserCollectSpuService.queryPage(params);

        return R.ok().put("page", page);
    }

}
